﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.UI.Xaml.Navigation;
using dwindleUrl.Message;
using dwindleUrl.Models;
using dwindleUrl.Repositories;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.Mvvm.Interfaces;
using Microsoft.Practices.Prism.PubSubEvents;

namespace dwindleUrl.ViewModels
{
    public class MainPageViewModel : ViewModel, IMainPageViewModel
    {
        private readonly INavigationService _navigationService;
        private readonly Repositories.IItemRepository _itemRepository ;
        private IEventAggregator EventAgregator { get; set; }

        public MainPageViewModel(INavigationService navigationService,
                                 Repositories.IItemRepository itemRepository,
                                 IEventAggregator eventAggregator)
        {
            EventAgregator = eventAggregator;
            _navigationService = navigationService;
            _itemRepository = itemRepository;
        }

        private ObservableCollection<Models.Item> _Items = new ObservableCollection<Item>();
        public ObservableCollection<Models.Item> Items { get { return _Items; } }

        private Models.Item _selected = default(Models.Item);
        public Models.Item Selected { get { return _selected; } set { SetProperty(ref _selected, value); } }

        private DelegateCommand _aboutCommand = null;
        public DelegateCommand AboutCommand
        {
            get
            {
                if (_aboutCommand != null)
                    return _aboutCommand;
                _aboutCommand = new DelegateCommand
                (
                    () => { _navigationService.Navigate("About", null); },
                    () => true);
                this.PropertyChanged += (s, e) => _aboutCommand.RaiseCanExecuteChanged();
                return _aboutCommand;
            }
        }

        private DelegateCommand _openCommand = null;
        public DelegateCommand OpenCommand
        {
            get
            {
                if (_openCommand != null)
                    return _openCommand;
                _openCommand = new DelegateCommand
                    (
                    () => { _navigationService.Navigate("Details", Selected.Text); },
                    () => { return this.Selected != null; }
                    );
                this.PropertyChanged += (s, e) => _openCommand.RaiseCanExecuteChanged();
                return _openCommand;
            }
        }

        private int _value = default(int);
        public int Value { get { return _value; } set { SetProperty(ref _value, value); } }

        public override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode,
            Dictionary<string, object> viewModelState)
        {
            foreach (var item in _itemRepository.AllItems())
            {
                this.Items.Add(item);
            }
            EventAgregator.GetEvent<NewValue>().Subscribe(HandleValueChanged);
        }

        private void HandleValueChanged(int value)
        {
            this.Value = value;
        }
    }
}
